﻿using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.Menu.Dtos;
using StarsAdmin.Application.Menu.Services;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(4)]
    public class MenuController : BaseController
    {
        private readonly IMenuService _menuService;

        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<MenuDto>>> GetAllAsync([FromQuery] BaseQueryInput input)
        {
            return (await _menuService.GetAllAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<Page<MenuDto>>> GetPageAsync([FromQuery] PageQueryInput input)
        {
            return (await _menuService.GetPageAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<MenuDto>> GetByIdAsync([Required] long id)
        {
            return (await _menuService.GetByIdAsync(id)).ResultFormat("没有找到该条数据", 404);
        }

        /// <summary>
        /// 根据Id集合获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<MenuDto>>> GetByIdsAsync([FromQuery] BaseIdsInput input)
        {
            return (await _menuService.GetByIdsAsync(input.Ids)).ResultFormat();
        }

        /// <summary>
        /// 导出模版
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportTemplateAsync()
        {
            var result = await _menuService.ExportTemplateAsync();
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 批量导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAsync(BaseIdsInput input)
        {
            var result = await _menuService.ExportAsync(input.Ids);
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 批量导入
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> ImportAsync(ImportMenuInput input)
        {
            return (await _menuService.ImportAsync(input.Menus)).ResultFormat("批量导入失败");
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> AddAsync(AddMenuInput input)
        {
            return (await _menuService.AddAsync(input)).ResultFormat("添加失败");
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> AssignPermissionsAsync(AssignPermissionsInput input)
        {
            return (await _menuService.AssignPermissionsAsync(input)).ResultFormat("分配权限失败");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> UpdateAsync(UpdateMenuInput input)
        {
            return (await _menuService.UpdateAsync(input)).ResultFormat("更新失败");
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> SortableAsync(SortableMenuInput input)
        {
            return (await _menuService.SortableAsync(input.Id, input.TargetId)).ResultFormat("排序失败");
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> EnabledAsync([Required] long id)
        {
            return (await _menuService.EnabledAsync(id)).ResultFormat("切换状态失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> DeleteAsync([Required] long id)
        {
            return (await _menuService.DeleteAsync(id)).ResultFormat("删除失败");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> BatchDeleteAsync(BaseIdsInput input)
        {
            return (await _menuService.BatchDeleteAsync(input.Ids)).ResultFormat("批量删除失败");
        }
    }
}